﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCSLibrary;
using System.Data;
using Utils.DC.Web.UI;
using System.Collections;

public partial class Admin_Form_FunctionPage_Add : System.Web.UI.Page
{
    FunctionPageUI objFunctionPage = new FunctionPageUI();
    DataSet dsFunctionPage = new DataSet();
    MessageBox messageBox;

    protected void Page_Load(object sender, EventArgs e)
    {
        messageBox = new MessageBox();
        if (!IsPostBack)
        {
            BindFunctionType();
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
        }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        //bien page-alias
        string alias = Utils.Format.ConvertToNoSign(txtFunctionPageName.Text.Trim());

        if (txtFunctionPageID.Value.Trim() == "")
        {
            try
            {
                int result = objFunctionPage.Add(txtFunctionPageName.Text, alias, txtFunctionPageTitle.Text, txtFunctionPageImage.Text, txtFunctionPageIcon.Text,
                    txtFunctionPageDescription.Content, txtMetaKeyword.Text, txtMetaDescription.Text, chkIsVisible.Checked, chkIsAllowDelete.Checked, Convert.ToInt32(txtViewOrder.Text), cboLanguage.SelectedValue,
                    chkIsAdmin.Checked, cboFunctionType.SelectedValue, chkIsCache.Checked, Convert.ToInt32(txtCacheTime.Text), txtControlPath.Text.Trim(), GenUrlPath(alias));

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
                    //BindFunctionPage();
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
            }
        }
        else
        {
            try
            {
                int result = objFunctionPage.Update(Convert.ToInt32(txtFunctionPageID.Value), txtFunctionPageName.Text, alias, txtFunctionPageTitle.Text, txtFunctionPageImage.Text, txtFunctionPageIcon.Text,
                    txtFunctionPageDescription.Content, txtMetaKeyword.Text, txtMetaDescription.Text, chkIsVisible.Checked, chkIsAllowDelete.Checked, Convert.ToInt32(txtViewOrder.Text), cboLanguage.SelectedValue,
                    chkIsAdmin.Checked, cboFunctionType.SelectedValue, chkIsCache.Checked, Convert.ToInt32(txtCacheTime.Text), txtControlPath.Text.Trim(), GenUrlPath(alias));

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                    //BindFunctionPage();
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/quan-tri-chung/quan-tri-trang-chuc-nang.aspx");
    }

    #region Cac Ham Thao Tac Du Lieu

    private void ReInsertData()
    {
        //txtFunctionPageID.Value = "";
        txtCacheTime.Text = "0";
        txtFunctionPageDescription.Content = "";
        txtFunctionPageIcon.Text = "";
        txtFunctionPageImage.Text = "";
        txtFunctionPageName.Text = "";
        txtFunctionPageTitle.Text = "";
        txtMetaDescription.Text = "";
        txtMetaKeyword.Text = "";
        txtViewOrder.Text = "0";
        chkIsAdmin.Checked = false;
        chkIsAllowDelete.Checked = false;
        chkIsCache.Checked = false;
        chkIsVisible.Checked = false;

        cboLanguage.SelectedIndex = 0;
        cboFunctionType.SelectedIndex = 0;
    }

    private void LoadEditData(int FunctionPageID)
    {
        DataSet ds = objFunctionPage.GetByID(FunctionPageID);
        if (ds != null && ds.Tables.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtFunctionPageID.Value = FunctionPageID.ToString();
            txtCacheTime.Text = row["CacheTime"].ToString();
            txtFunctionPageDescription.Content = row["FunctionPageDescription"].ToString();
            txtFunctionPageIcon.Text = row["FunctionPageIcon"].ToString();
            txtFunctionPageImage.Text = row["FunctionPageImage"].ToString();
            txtFunctionPageName.Text = row["FunctionPageName"].ToString();
            txtFunctionPageTitle.Text = row["FunctionPageTitle"].ToString();
            txtMetaDescription.Text = row["MetaDescription"].ToString();
            txtMetaKeyword.Text = row["MetaKeyword"].ToString();
            txtViewOrder.Text = row["ViewOrder"].ToString();

            txtControlPath.Text = "";
            //Kiem tra neu control path la tu dong naviagate thi khong can phai bind lai vao textbox
            //De sau khi cap nhat, neu co thay ten trang thi control sua lai tu dong duoc cap nhat lai
            /*
            if (row["ControlPath"].ToString().IndexOf(row["FunctionPageName"].ToString() + ".aspx") == -1)
            {
                txtControlPath.Text = row["ControlPath"].ToString().Trim();
            }
            else
            {
                txtControlPath.Text = "";
            }*/

            chkIsAdmin.Checked = Convert.ToBoolean(row["IsAdmin"]);
            chkIsAllowDelete.Checked = Convert.ToBoolean(row["AllowDelete"]);
            chkIsCache.Checked = Convert.ToBoolean(row["IsCache"]);
            chkIsVisible.Checked = Convert.ToBoolean(row["IsVisible"]);

            IEnumerator ie = cboLanguage.Items.GetEnumerator();
            int index = 0;
            ListItem item = null;
            while (ie.MoveNext())
            {
                item = (ListItem)ie.Current;
                if (item.Value == row["Lang"].ToString())
                {
                    cboLanguage.SelectedIndex = index;
                    break;
                }
                index++;
            }

            ie = cboFunctionType.Items.GetEnumerator();
            index = 0;
            while (ie.MoveNext())
            {
                item = (ListItem)ie.Current;
                if (item.Value == row["FunctionType"].ToString().Trim())
                {
                    cboFunctionType.SelectedIndex = index;
                    break;
                }
                index++;
            }
            btnCreate.Text = "Cập nhật";
        }
    }

    private void BindFunctionType()
    {
        DataSet ds = new PropertiesUI().GetByPropertyTypeName("FunctionType");
        if (ds != null && ds.Tables.Count > 0)
        {
            cboFunctionType.DataSource = ds.Tables[0];
            cboFunctionType.DataBind();
        }
    }

    #endregion

    #region Cac Ham Khac

    private String GenUrlPath(string functionPageName)
    {
        if (chkIsAdmin.Checked)
        {
            return "/Admin/" + functionPageName + ".aspx";
        }
        else
        {
            return "/" + functionPageName + ".aspx";
        }
    }
    #endregion
}
